﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restarty_SIPO3.Forms;
using DB;
using System.Collections;
using Restarty_SIPO3.Class;

namespace Restarty_SIPO3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            refreshSuborov();
            najdenyZakaznik = new ArrayList();
        }

        private void koniecToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void pridanieSúborovToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fbdSpravaSuborov.ShowDialog() == DialogResult.OK)
            {
                Nacitavanie frmNacitavanie = new Nacitavanie(fbdSpravaSuborov.SelectedPath);
                frmNacitavanie.ShowDialog();
                refreshSuborov();
            }
        }

        private void refreshSuborov()
        {
            if (!bwPracaSDB.IsBusy)
                bwPracaSDB.RunWorkerAsync("NacitajSubory");
        }

        private void tmTest_Tick(object sender, EventArgs e)
        {
            if (!bwTest.IsBusy)
                bwTest.RunWorkerAsync();
        }

        private void bwTest_DoWork(object sender, DoWorkEventArgs e)
        {
            prihlasenieNaDB databaza = new prihlasenieNaDB();
            Online = databaza.testDB();
        }

        public bool Online { get; set; }

        private void bwTest_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (Online)
            {
                tsslStavDatabazy.Image = Properties.Resources.Online_lime_icon;
            }
            else
            {
                tsslStavDatabazy.Image = Properties.Resources.Online_strawberry_icon;
            }
        }

        private void nastavenieDatabázyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NastaveniaWindow frmNastav = new NastaveniaWindow();
            frmNastav.ShowDialog();
        }

        private void tsbZmazDB_Click(object sender, EventArgs e)
        {
            prihlasenieNaDB databaza = new prihlasenieNaDB();
            databaza.vykonajSQLfunkciu("CALL zmazDatabazu();");
            MessageBox.Show(this, "Databaza zmazana", "Uloha dokoncena",MessageBoxButtons.OK ,MessageBoxIcon.Information);
            refreshSuborov();
        }

        private void bwPracaSDB_DoWork(object sender, DoWorkEventArgs e)
        {
            switch (e.Argument.ToString())
            {
                case "NacitajSubory": nacitajSubory();
                    break;
                case "HladajKod": hladajKod();
                    break;
            }
            e.Result = e.Argument;
        }

        private void hladajKod()
        {
            prihlasenieNaDB databaza = new prihlasenieNaDB();
            najdenyZakaznik = databaza.vratSQLreader("CALL hladajZaznam('" + NacitanyKod + "');");
        }

        private void nacitajSubory()
        {
            ZoznamSuborov = new ArrayList();
            prihlasenieNaDB databaza = new prihlasenieNaDB();
            ZoznamSuborov = databaza.vratSQLreader("CALL vratSubory();");
            if (databaza.IsError)
            {
                MessageBox.Show(databaza.ErrorText, "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        public ArrayList ZoznamSuborov { get; set; }

        private void bwPracaSDB_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            switch (e.Result.ToString())
            {
                case "NacitajSubory":
                    {
                        lbSubory.Items.Clear();
                        foreach (ArrayList tmpArray in ZoznamSuborov)
                        {
                            lbSubory.Items.Add(tmpArray[0].ToString());
                        }
                    }
                    break;
                case "HladajKod":
                    {
                        nastavHodnoty();
                        tbKod.Enabled = true;
                        tbKod.Text = "";
                        tbKod.Focus();
                    }
                    break;
            }
        }

        private void nastavHodnoty()
        {
            if(najdenyZakaznik.Count>0)
            {
                ArrayList tmpArray = (ArrayList)najdenyZakaznik[0];
                tbMeno.Text = tmpArray[3].ToString();
                tbUlica.Text = tmpArray[4].ToString();
                tbMesto.Text = tmpArray[5].ToString();
                tbSuma.Text = tmpArray[7].ToString() + tmpArray[8].ToString();
                najdenyZakaznik = new ArrayList();
            }
        }

        private void tbKod_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = SpracovanieRetazca.kontrolaKodu(e.KeyChar);
            if (e.KeyChar == (char)13)
            {
                tbKod.Enabled = false;
                NacitanyKod = tbKod.Text;
                if (!bwPracaSDB.IsBusy)
                    bwPracaSDB.RunWorkerAsync("HladajKod");
                else
                {
                    tbKod.Enabled = true;
                    tbKod.Text = "";
                    tbKod.Focus();
                }
            }
        }

        public string NacitanyKod { get; set; }

        public ArrayList najdenyZakaznik { get; set; }
    }
}
